#include <glib.h>
#include <libgnomevfs/gnome-vfs.h>

/*
 * The Kew interface to this allows a URI to be created using the URI factory object.
 * Creation does not imply that access is allowed.  The application has no way of
 * knowing whether it is allowed to access the URI, and if so, in what modes.  This is
 * so that malicious code cannot easily hide its true behaviour.
 *
 * URI new "http://ikew.org/packages/foo.kew" :FooURI.
 * FooURI reader :FooReader.
 * FooURI readAll :Contents.
 * FooURI list :FooList.
 * FooURI & "bar.kew" :BarURI.
 * FooURI & "../help/index.html" :HelpURI.
 * 
 * FooURI moveTo BarURI.
 *
 * TODO: the asynchronous interfaces (see gnome-vfs-async)
 
 * URI new "file:///tmp/foo"
 *   create IsWorldReadable IsWorldWritable
 *   createIfAbsent IsWorldReadable IsWorldWritable
 *     reader
 *     writer
 *
 * File home
 *      root
 *      desktop
 *      favorites
 * then
 *      type
 *      children
 *      size
 *      name
 *      parent
 *      reader
 *      writer
 *      activate
 *
 * Ideally files should have a DOM-like structure that is the same for all tree
 * structured data.
 */

typedef struct _KewURIObject KewURIObject;
typedef struct _KewVFSHandleObject KewVFSHandleObject;
    
struct _KewURIObject {
    KewObject super;
    GnomeVFSURI *vfs_uri;
    
};

struct _KewVFSHandleObject {
    KewObject super;
    GnomeVFSHandle *vfs_handle;

};

/* FIXME: need to use these finalizers OR use libGC (or both) */

static void uri_finalizer(KewUriObject *uri_object)
{
    gnome_vfs_uri_unref(uri_object->vfs_uri);
}

static void vfs_handle_finalizer(KewVFSHandle *vfs_handle_object)
{
    gnome_vfs_handle_unref(vfs_handle_object->vfs_handle;
}

KewUriObject *kew_uri_wrap(GnomeVFSURI *vfs_uri)
{
    KewUriObject *result;
    
    /* FIXME: this is really relying on call-order side effects which isn't nice */
    if (!vfs_uri)
        g_error("Badly formatted URI"); /* FIXME: should raise an exception here ?? but should this be thrown into C code */
    
    result = kew_object_new(KewUriObject, 0);
    result->vfs_uri = vfs_uri;
    return result;
}

KewUriObject *kew_uri_new(const gchar *uri)
{
    return kew_uri_wrap(gnome_vfs_uri_new(uri));
}

KEW_METHOD(kew_uri_factory_new)
{
    results[0] = kew_uri_new(kew_string_value(args[0]));
}

KEW_METHOD(kew_uri_string)
{
    results[0] = kew_string_wrap(gnome_vfs_uri_to_string(receiver->vfs_uri, GNOME_VFS_URI_HIDE_NONE));
}

KEW_METHOD(kew_uri_append) /* maps to & selector */
{
    results[0] = kew_uri_wrap(gnome_vfs_uri_resolve_relative(receiver->vfs_uri, kew_string_value(args[0])));
}

KEW_METHOD(kew_uri_reader)
{
    GnomeVFSHandle *handle;
    GnomeVFSResult result = gnome_vfs_open_uri(&handle, receiver->vfs_uri, 
    results[0] = kew_vfs_handle_wrap(
}
